package com.lfk.justweengine.utils.music;

import ohos.app.Context;
import ohos.global.resource.RawFileDescriptor;
import ohos.media.player.Player;

import java.io.IOException;

/**
 * MusicPlayer
 *
 * @author liufengkai
 *         Created by liufengkai on 16/2/5.
 */
public class MusicPlayer implements Music, Player.IPlayerCallback {
    private Player player;
    // 准备好?
    private boolean isPrepared;
    // 是否循环
    private boolean isLooping;
    // 音量
    private float volume;

    public MusicPlayer(Context context, String fileName) {
        player = new Player(context);
        // init
        initMusicPlayer();
        try {
            // get file descriptor
            RawFileDescriptor descriptor = context.getResourceManager().getRawFileEntry(fileName).openRawFileDescriptor();
            // setData
            player.setSource(descriptor);
            player.prepare();
            isPrepared = true;
            player.setPlayerCallback(this);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Couldn't load music");
        }
    }

    private void initMusicPlayer() {
        volume = 10;
        isPrepared = false;
        isLooping = false;
    }

    @Override
    public void play() {
        if (player.isNowPlaying())
            return;

        synchronized (this) {
            if (isPrepared) {
                player.prepare();
            }
            player.play();
        }

    }

    @Override
    public void stop() {
        player.stop();
        synchronized (this) {
            isPrepared = false;
        }
    }

    @Override
    public void pause() {
        if (player.isNowPlaying())
            player.pause();
    }

    @Override
    public void setLooping(boolean isLooping) {
        this.isLooping = isLooping;
        if (player != null)
            player.enableSingleLooping(isLooping);
    }

    @Override
    public void setVolume(float volume) {
        this.volume = volume;

        if (player != null)
            player.setVolume(volume);
    }

    @Override
    public float getVolume() {
        return volume;
    }

    @Override
    public boolean isPlaying() {
        return player.isNowPlaying();
    }

    @Override
    public boolean isLooping() {
        return isLooping;
    }

    @Override
    public void dispose() {
        if (player.isNowPlaying()) {
            player.stop();
        }
        player.release();
    }

    @Override
    public void onPrepared() {
        synchronized (this) {
            isPrepared = false;
        }
    }

    @Override
    public void onMessage(int i, int i1) {

    }

    @Override
    public void onError(int i, int i1) {

    }

    @Override
    public void onResolutionChanged(int i, int i1) {

    }

    @Override
    public void onPlayBackComplete() {

    }

    @Override
    public void onRewindToComplete() {

    }

    @Override
    public void onBufferingChange(int i) {

    }

    @Override
    public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

    }

    @Override
    public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

    }
}
